# -*- coding: utf-8 -*-
#  @author  Bink
#  @date  2020/10/22 2:32 下午
#  @Email : 2641032316@qq.com

# todo
#  进行登陆、注册、修改密码、删除用户等操作

import pymysql


def showData():
    cursor.execute("select * from user")
    con.commit()
    for row in cursor.fetchall():
        for col in row:
            print(col, end=" ")
        print()
    print()


# 登录
con = pymysql.connect(host="localhost",
                      user="bink",
                      passwd="",
                      db="python-programming",
                      charset="utf8")
if con is not None:
    print("登录成功！")
cursor = con.cursor()

# 注册（添加）
sql = "insert into user (username, password) values (%s, %s)"
param = ('tmp—user', '000000')
res = cursor.execute(sql, param)
con.commit()
if res > 0:
    print("注册成功！")
    showData()
else:
    print("注册失败！")

# 修改密码
sql = "update user set password=%s where username=%s"
param = ('111111', 'tmp—user')
res = cursor.execute(sql, param)
con.commit()
if res > 0:
    print("修改成功！")
    showData()
else:
    print("修改失败！")

# 删除用户
sql = "delete from user where username=%s and password=%s"
param = ('tmp—user', '111111')
res = cursor.execute(sql, param)
con.commit()
if res > 0:
    print("删除成功！")
    showData()
else:
    print("删除失败！")



con.close()
